/*   1:    */ package org.intellij.lang.regexp.psi.impl;
/*   2:    */ 
/*   3:    */

import com.intellij.lang.ASTNode;
import com.intellij.psi.StringEscapesTokenTypes;
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.tree.TokenSet;
import org.intellij.lang.regexp.RegExpTT;
import org.intellij.lang.regexp.psi.RegExpChar;
import org.intellij.lang.regexp.psi.RegExpElementVisitor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */ 
/*  15:    */ public class RegExpCharImpl
/*  16:    */   extends RegExpElementImpl
/*  17:    */   implements RegExpChar
/*  18:    */ {
/*  19: 31 */   private static final TokenSet e = TokenSet.create(new IElementType[] { RegExpTT.OCT_CHAR, RegExpTT.BAD_OCT_VALUE });
/*  20: 32 */   private static final TokenSet f = TokenSet.create(new IElementType[] { RegExpTT.HEX_CHAR, RegExpTT.BAD_HEX_VALUE });
/*  21: 33 */   private static final TokenSet g = TokenSet.create(new IElementType[] { RegExpTT.HEX_CHAR, StringEscapesTokenTypes.INVALID_UNICODE_ESCAPE_TOKEN });
/*  22:    */   
/*  23:    */   public RegExpCharImpl(ASTNode paramASTNode)
/*  24:    */   {
/*  25: 36 */     super(paramASTNode);
/*  26:    */   }
/*  27:    */   
/*  28:    */   @NotNull
/*  29:    */   public RegExpChar.Type getType()
/*  30:    */   {
/*  31: 41 */     ASTNode localASTNode = getNode().getFirstChildNode();
/*  32: 42 */     assert (localASTNode != null);
/*  33: 43 */     IElementType localIElementType = localASTNode.getElementType();
/*  34: 44 */     if (e.contains(localIElementType))
/*  35:    */     {
/*  36: 45 */       RegExpChar.Type tmp48_45 = RegExpChar.Type.OCT;
/*  37: 45 */       if (tmp48_45 == null) {
/*  38: 45 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/lang/regexp/psi/impl/RegExpCharImpl", "getType" }));
/*  39:    */       }
/*  40: 45 */       return tmp48_45;
/*  41:    */     }
/*  42: 46 */     if (f.contains(localIElementType))
/*  43:    */     {
/*  44: 47 */       RegExpChar.Type tmp96_93 = RegExpChar.Type.HEX;
/*  45: 47 */       if (tmp96_93 == null) {
/*  46: 47 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/lang/regexp/psi/impl/RegExpCharImpl", "getType" }));
/*  47:    */       }
/*  48: 47 */       return tmp96_93;
/*  49:    */     }
/*  50: 48 */     if (g.contains(localIElementType))
/*  51:    */     {
/*  52: 49 */       RegExpChar.Type tmp144_141 = RegExpChar.Type.UNICODE;
/*  53: 49 */       if (tmp144_141 == null) {
/*  54: 49 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/lang/regexp/psi/impl/RegExpCharImpl", "getType" }));
/*  55:    */       }
/*  56: 49 */       return tmp144_141;
/*  57:    */     }
/*  58: 50 */     if (localIElementType == TokenType.ERROR_ELEMENT)
/*  59:    */     {
/*  60: 51 */       RegExpChar.Type tmp189_186 = RegExpChar.Type.INVALID;
/*  61: 51 */       if (tmp189_186 == null) {
/*  62: 51 */         throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/lang/regexp/psi/impl/RegExpCharImpl", "getType" }));
/*  63:    */       }
/*  64: 51 */       return tmp189_186;
/*  65:    */     }
/*  66: 53 */     RegExpChar.Type tmp227_224 = RegExpChar.Type.CHAR;
/*  67: 53 */     if (tmp227_224 == null) {
/*  68: 53 */       throw new IllegalStateException(String.format("@NotNull method %s.%s must not return null", new Object[] { "org/intellij/lang/regexp/psi/impl/RegExpCharImpl", "getType" }));
/*  69:    */     }
/*  70: 53 */     return tmp227_224;
/*  71:    */   }
/*  72:    */   
/*  73:    */   @Nullable
/*  74:    */   public Character getValue()
/*  75:    */   {
/*  76: 59 */     String str = getUnescapedText();
/*  77: 60 */     if ((str.equals("\\")) && (getType() == RegExpChar.Type.CHAR)) {
/*  78: 61 */       return Character.valueOf('\\');
/*  79:    */     }
/*  80: 64 */     if ((str.startsWith("\\")) && (str.length() > 1))
/*  81:    */     {
/*  82: 65 */       ASTNode localASTNode = getNode().getFirstChildNode();
/*  83: 66 */       assert (localASTNode != null);
/*  84: 67 */       IElementType localIElementType = localASTNode.getElementType();
/*  85: 68 */       if (localIElementType == RegExpTT.OCT_CHAR) {
/*  86:    */         try
/*  87:    */         {
/*  88: 70 */           return Character.valueOf((char)Integer.parseInt(str.substring(1), 8));
/*  89:    */         }
/*  90:    */         catch (NumberFormatException localNumberFormatException) {}
/*  91:    */       }
/*  92:    */     }
/*  93: 77 */     return unescapeChar(str);
/*  94:    */   }
/*  95:    */   
/*  96:    */   @Nullable
/*  97:    */   static Character unescapeChar(String paramString)
/*  98:    */   {
/*  99: 82 */     assert (paramString.length() > 0);
/* 100:    */     
/* 101: 84 */     int i = 0;
/* 102: 85 */     for (int j = 0; j < paramString.length(); j++)
/* 103:    */     {
/* 104: 86 */       char c = paramString.charAt(j);
/* 105: 87 */       if (i == 0)
/* 106:    */       {
/* 107: 88 */         if (c == '\\') {
/* 108: 89 */           i = 1;
/* 109:    */         } else {
/* 110: 91 */           return Character.valueOf(c);
/* 111:    */         }
/* 112:    */       }
/* 113:    */       else
/* 114:    */       {
/* 115: 94 */         switch (c)
/* 116:    */         {
/* 117:    */         case 'n': 
/* 118: 96 */           return Character.valueOf('\n');
/* 119:    */         case 'r': 
/* 120: 98 */           return Character.valueOf('\r');
/* 121:    */         case 't': 
/* 122:100 */           return Character.valueOf('\t');
/* 123:    */         case 'a': 
/* 124:102 */           return Character.valueOf('\007');
/* 125:    */         case 'e': 
/* 126:104 */           return Character.valueOf('\033');
/* 127:    */         case 'f': 
/* 128:106 */           return Character.valueOf('\f');
/* 129:    */         case 'b': 
/* 130:108 */           return Character.valueOf('\b');
/* 131:    */         case 'c': 
/* 132:110 */           return Character.valueOf((char)(c ^ 0x40));
/* 133:    */         case 'x': 
/* 134:112 */           return parseNumber(j, paramString, 16, 2, true);
/* 135:    */         case 'u': 
/* 136:114 */           return parseNumber(j, paramString, 16, 4, true);
/* 137:    */         case '0': 
/* 138:116 */           return parseNumber(j, paramString, 8, 3, false);
/* 139:    */         }
/* 140:118 */         if (Character.isLetter(c)) {
/* 141:119 */           return null;
/* 142:    */         }
/* 143:121 */         return Character.valueOf(c);
/* 144:    */       }
/* 145:    */     }
/* 146:126 */     return null;
/* 147:    */   }
/* 148:    */   
/* 149:    */   static Character parseNumber(int paramInt1, String paramString, int paramInt2, int paramInt3, boolean paramBoolean)
/* 150:    */   {
/* 151:130 */     int i = paramInt1 + 1;
/* 152:131 */     int j = i + paramInt3;
/* 153:    */     try
/* 154:    */     {
/* 155:133 */       int k = 0;
/* 156:135 */       for (int m = i; (m < j) && (m < paramString.length()); m++)
/* 157:    */       {
/* 158:136 */         k *= paramInt2;
/* 159:137 */         k += Integer.valueOf(paramString.substring(m, m + 1), paramInt2).intValue();
/* 160:    */       }
/* 161:139 */       if (m - i == 0) {
/* 162:139 */         return null;
/* 163:    */       }
/* 164:140 */       return (m - i < paramInt3) && (paramBoolean) ? null : Character.valueOf((char)k);
/* 165:    */     }
/* 166:    */     catch (NumberFormatException localNumberFormatException) {}
/* 167:142 */     return null;
/* 168:    */   }
/* 169:    */   
/* 170:    */   public void accept(RegExpElementVisitor paramRegExpElementVisitor)
/* 171:    */   {
/* 172:147 */     paramRegExpElementVisitor.visitRegExpChar(this);
/* 173:    */   }
/* 174:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.lang.regexp.psi.impl.RegExpCharImpl

 * JD-Core Version:    0.7.0.1

 */